﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.Collections.Generic;
using System.IO;
using System.Data;

using AJRavindiran.Jolt.Storage;

namespace AJRavindiran.Jolt.RuneScape.Models.Items
{
    /// <summary>
    /// Represents a item definition loader.
    /// </summary>
    public class ItemDefinitionLoader
    {
        /// <summary>
        /// Load all items from the database.
        /// </summary>
        /// <param name="itemDefinitions">The dictionary to want to store definitions in.</param>
        public static void Load(Dictionary<int, ItemDefinition> itemDefinitions)
        {
            try
            {
                DataTable dataTable = null;
                using (DatabaseClient dbClient = Jolt.GetDatabase().GetClient())
                {
                    dataTable = dbClient.ReadDataTable("SELECT * FROM item_definitions;");
                }

                foreach (DataRow row in dataTable.Rows)
                {
                    var definition = new ItemDefinition();

                    // General definitions.
                    definition.Id = Convert.ToInt32(row[0]);
                    definition.Name = Convert.ToString(row[1]);
                    definition.Examine = Convert.ToString(row[2]);
                    definition.EquipId = Convert.ToInt32(row[3]);
                    definition.Stackable = Convert.ToBoolean(row[4]);
                    definition.Noted = Convert.ToBoolean(row[5]);

                    // Price definitions.
                    definition.Price = new ItemDefinitionPrice();
                    definition.Price.MinimumPrice = Convert.ToInt32(row[6]);
                    definition.Price.NormalPrice = Convert.ToInt32(row[7]);
                    definition.Price.MaximumPrice = Convert.ToInt32(row[8]);

                    // Bonus definitions.
                    definition.Bonus = new ItemDefinitionBonus();
                    definition.Bonus.AttackStabBonus = Convert.ToInt16(row[9]);
                    definition.Bonus.AttackSlashBonus = Convert.ToInt16(row[10]);
                    definition.Bonus.AttackCrushBonus = Convert.ToInt16(row[11]);
                    definition.Bonus.AttackMagicBonus = Convert.ToInt16(row[12]);
                    definition.Bonus.AttackRangedBonus = Convert.ToInt16(row[13]);
                    definition.Bonus.DefenceStabBonus = Convert.ToInt16(row[14]);
                    definition.Bonus.DefenceSlashBonus = Convert.ToInt16(row[15]);
                    definition.Bonus.DefenceCrushBonus = Convert.ToInt16(row[16]);
                    definition.Bonus.DefenceMagicBonus = Convert.ToInt16(row[17]);
                    definition.Bonus.DefenceRangedBonus = Convert.ToInt16(row[18]);
                    definition.Bonus.DefenceSummoningBonus = Convert.ToInt16(row[19]);
                    definition.Bonus.StrengthBonus = Convert.ToInt16(row[20]);
                    definition.Bonus.PrayerBonus = Convert.ToInt16(row[21]);

                    // Add the definitions to dictionary.
                    itemDefinitions.Add(definition.Id, definition);
                }
            }
            catch (Exception ex)
            {
                Jolt.GetLog().WriteException(ex);
            }
        }
    }
}
